package com.guoq.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guoq.constant.Configration;
import com.guoq.pojo.EUDataGridResult;
import com.guoq.pojo.Orders;
import com.guoq.pojo.Result;
import com.guoq.pojo.User;
import com.guoq.service.IOrderService;
import com.guoq.utils.Strkit;


/**
 * 
 * <P>类名称:OrderController
 * <P>类描述: 订单 Controller
 * <P>创建人:郭强  
 * <P>修改人:XXX 
 * <P>修改时间:2017年7月20日 上午11:59:04
 * <P>修改备注: 
 * @version 1.0.0
 * Copyright(c)2017西部在线-版权所有
 */
@Controller
@RequestMapping("/order")
public class OrderController {
	@Resource
	private IOrderService orderService;
	
	@RequestMapping("")
	public String toIndex(HttpServletRequest request,Model model){
		
		EUDataGridResult result = threeMonthList(request);
		model.addAttribute("result", result);
		return "/order/list";
	}
	
	@RequestMapping("/toadd")
	public String toAdd(HttpServletRequest request,Model model){
		return "/order/add";
	}
	
	@RequestMapping("/toedit")
	public String toEdit(HttpServletRequest request,Model model){
		return "/order/edit";
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: getOrderById
	 *<P>描述: 根据id获取类目信息
	 *@param request
	 *@param model
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/getOrderById")
	@ResponseBody
	public Orders getOrderById(HttpServletRequest request,Model model){
		int id = Integer.parseInt(request.getParameter("id"));
		Orders record = this.orderService.getById(id);
		return record;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: list
	 *<P>描述: 获取订单列表（不分页）
	 *@param request
	 *@param model
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/list")
	@ResponseBody
	public List<Orders> list(HttpServletRequest request,Model model){
		
		List<Orders> list = this.orderService.getList();
		return list;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: pageListByCustId
	 *<P>描述: 根据顾客用户id获取订单列表（分页）
	 *@param page
	 *@param rows
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/pageListByCustId")
	@ResponseBody
	public EUDataGridResult pageListByCustId(HttpServletRequest request, Integer page, Integer rows) {
		
		if(Strkit.notBlank("" + page)){
			page = 1;
		}
		if(Strkit.notBlank("" + rows)){
			rows = Configration.PAGESIZE;
		}
		User user = (User) request.getSession().getAttribute("user");
		String user_id = user.getUserId();
		EUDataGridResult result = orderService.getListByCustId(page, rows, user_id);
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: threeMonthList
	 *<P>描述: 获取3个月内订单数据
	 *@param request
	 *@param page
	 *@param rows
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/threeMonthList")
	@ResponseBody
	public EUDataGridResult threeMonthList(HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute("user");
		String user_id = user.getUserId();
		EUDataGridResult result = orderService.getThreeMonthList(user_id);
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: threeMonthAgoList
	 *<P>描述: 获取3个月之前订单数据
	 *@param request
	 *@param page
	 *@param rows
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/threeMonthAgoList")
	@ResponseBody
	public EUDataGridResult threeMonthAgoList(HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute("user");
		String user_id = user.getUserId();
		EUDataGridResult result = orderService.getThreeMonthAgoList(user_id);
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: waitEvaluateList
	 *<P>描述: 待评价订单数据列表
	 *@param request
	 *@param page
	 *@param rows
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/waitEvaluateList")
	@ResponseBody
	public EUDataGridResult waitEvaluateList(HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute("user");
		String user_id = user.getUserId();
		EUDataGridResult result = orderService.waitEvaluateList(user_id);
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: cancelList
	 *<P>描述: 取消订单数据列表
	 *@param request
	 *@param page
	 *@param rows
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/cancelList")
	@ResponseBody
	public EUDataGridResult cancelList(HttpServletRequest request, Integer page, Integer rows) {
		
		User user = (User) request.getSession().getAttribute("user");
		String user_id = user.getUserId();
		EUDataGridResult result = orderService.waitEvaluateList(user_id);
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: getOrderById
	 *<P>描述: 根据orderid删除订单信息
	 *@param request
	 *@param model
	 *@return   
	 * @throws Exception 
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping("/deleteByOrderId")
	@ResponseBody
	public Result deleteByOrderId(HttpServletRequest request,Model model) throws Exception{
		
		String orderId = request.getParameter("order_id");
		Result result = this.orderService.deleteByOrderId(orderId);
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: pageListByShopId
	 *<P>描述: 根据店铺id获取订单列表（分页）
	 *@param page
	 *@param rows
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/pageListByShopId")
	@ResponseBody
	public EUDataGridResult pageListByShopId(HttpServletRequest request, Integer page, Integer rows) {
		
		if(Strkit.notBlank("" + page)){
			page = 1;
		}
		if(Strkit.notBlank("" + rows)){
			rows = Configration.PAGESIZE;
		}
		
		String shop_id = request.getParameter("shop_id");
		EUDataGridResult result = orderService.getListByShopId(page, rows, shop_id);
		return result;
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: add
	 *<P>描述: 新增
	 *@param order
	 *@return   
	 * @throws Exception 
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public Result add(HttpServletRequest request) throws Exception{
		
		User user = (User) request.getSession().getAttribute("user");
		Map<String, String[]> map = request.getParameterMap();
		Result result = this.orderService.create(map, user);
		return result;
	} 
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: update
	 *<P>描述: 编辑
	 *@param record
	 *@return
	 *@throws Exception   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public Result update(Orders bean) throws Exception{
		
		Result result = this.orderService.update(bean);
		return result;
	} 
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: delete
	 *<P>描述:	删除 
	 *@param id
	 *@return
	 *@throws Exception   
	 *@exception   
	 *@since  1.0.0
	 */
	@RequestMapping(value="/delete")
	@ResponseBody
	public Result delete(Orders bean) throws Exception{
		
		Result result = this.orderService.delete(bean);
		return result;
	}
	
}
